<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileController extends Controller
{
    public function image_upload(Request $request){
        $file = $request->file('file');
        //判断文件是否上传成功
        if ($file->isValid()) {
            //扩展名
            $ext = $file->getClientOriginalExtension();

            //临时绝对路径
            $realPath = $file->getRealPath();
            $dir = $request->post('type');
            $filename = $dir.'/'.uniqid() . '.' . $ext;

            $bool = Storage::disk('local')->put($filename, file_get_contents($realPath));

            //判断是否上传成功
            return JsonResponse(200,$bool,'上传成功',$filename);
        }else{
            return JsonResponse(3001,false,'上传失败');
        }
    }

    public function file_delete(Request $request){
        $disk = Storage::disk('local');
        $result = $disk->delete($request->post('file_name'));
        if ($result){
            return JsonResponse(200,true,'删除成功');
        }else{
            return JsonResponse(4002,false,'删除失败',$request->post('file_name'));
        }
    }
}
